package com.soku.wangheng.txtcopy.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class CopyFileService {
	
	private static CopyFileService I = new CopyFileService();
	
	private ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(4);
	private CopyFileService() {
		//单例
	}
	
	public boolean copyFile(String fromDirName, String toDirName, String fromExten, String toExten) {
		File fromDir = new File(fromDirName);
		File toDir = new File(toDirName);
		if(!fromDir.exists() || !toDir.exists() || !fromDir.isDirectory() || !toDir.isDirectory()){
			//选择路径不存在或者不是目录 TODO
			return false;
		}
		if(!fromExten.startsWith("\\.")) {
			fromExten = "." + fromExten;
		}
		if(!toExten.startsWith("\\.")) {
			toExten = "." + toExten;
		}
		File[] fromDirFiles = fromDir.listFiles();
		List<File> fromFiles = new ArrayList<File>();
		for(File fromFile : fromDirFiles) {
			if(isFileMatch(fromFile, fromExten)){
				fromFiles.add(fromFile);
			}
		}
		//TODO 初始化progressbar
		
		return true;
	}

	private boolean isFileMatch(File fromFile, String exten) {
		String fileExten = fromFile.getName().substring(fromFile.getName().indexOf("."));
		System.out.println(fileExten);
		return fileExten.equals(exten);
	}

	
	public static void main(String[] args) {
		CopyFileService service = new CopyFileService();
		File file = new File("d:/test.dat");
		System.out.println(service.isFileMatch(file, ".dat"));
	}
}
